package com.flsldl.mydb.backend.dm.page;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.flsldl.mydb.backend.dm.pageCache.PageCache;
/**
 * @ClassName PageImpl
 * @Description TODO
 * @Author @O_o @Date 2023/2/18 下午 2:23
 */
public class PageImpl implements Page{

    // 页面页号 从 1 开始
    private int pageNumber;
    // 页面实际包含的字节数据
    private byte[] data;
    // 标志页面是否为脏页面
    private boolean dirty;
    private Lock lock;

    // 方便在拿到 Page 的引用时可以快速对这个页面的缓存进行释放操作
    private PageCache pc;

    public PageImpl(int pageNumber, byte[] data, PageCache pc) {
        this.pageNumber = pageNumber;
        this.data = data;
        this.pc = pc;
        lock = new ReentrantLock();
    }

    // ----------------------------------(0v0)--> 分割线--------------------------------------------------------------

    public void lock() {
        lock.lock();
    }

    public void unlock() {
        lock.unlock();
    }

    public void release() {
        pc.release(this);
    }

    public void setDirty(boolean dirty) {
        this.dirty = dirty;
    }

    public boolean isDirty() {
        return dirty;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public byte[] getData() {
        return data;
    }


}
